package servlet.customer.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.MerchantApplication;
import service.MerchantApplicationManager;
import service.factory.ManagerFactory;
import util.GetMerchantDtoListUtil;
import util.JsonUtil;
import util.PropertiesUtils;
import dto.MerchantDto;

public class LoadMerchantApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantApplicationManager manager = null;
       
    public LoadMerchantApplicationServlet() {
        super();
        manager = ManagerFactory.getApplicationManagerInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "application/json;charset=UTF-8");
		List<MerchantApplication> mList = manager.getByStatus(MerchantApplication.WHITE_LIST);
		List<MerchantDto> mDtos = GetMerchantDtoListUtil.getMerchantDto(mList);
		for(MerchantDto m:mDtos){
			String path = PropertiesUtils.getProperties("image.get.path")+m.getAvatar();
			m.setAvatar(path);
		}
		PrintWriter writer = response.getWriter();
		if(mList.size()>0)
			writer.write(JsonUtil.toJsonString(mDtos));
		writer.close();
	}
}
